using RedPoint.Runtime;
using UnityEditor;
using UnityEditor.IMGUI.Controls;
using UnityEngine;

namespace Framework.RedPoint.Editor
{
    /// <summary>
    /// 红点树窗口
    /// </summary>
    public class ReddotTreeViewWindow :  EditorWindow
    {
        private static ReddotTreeViewWindow m_window;
        
        private SearchField m_SearchField;
        
        private ReddotTreeView  m_TreeView;
        
        [MenuItem("Window/红点树窗口")]
        private static void OpenWindow()
        {
            if (!Application.isPlaying || RedPointManager.Instance.Root.Childrens == null)
            {
                EditorUtility.DisplayDialog("警告！", "运行中才能打开红点树视图窗口", "OK");
                return;
            }

            m_window = GetWindow<ReddotTreeViewWindow>();
            m_window.titleContent = new GUIContent("红点树查看窗口");
            m_window.Show();
        }

        private void OnGUI()
        {
            // 顶部工具
            UpToolbar();

            TreeView();
            
            // 底部工具
            BottomToolBar();
        }

        private void OnEnable()
        {
            m_TreeView = new ReddotTreeView(new TreeViewState());
            
            // 创建搜索栏目
            m_SearchField = new SearchField();
            m_SearchField.downOrUpArrowKeyPressed += m_TreeView.SetFocusAndEnsureSelectedItem;
            
            EditorApplication.playModeStateChanged += OnPlayModeStateChange;
        }

        private void OnDisable()
        {
            m_TreeView.OnDestory();
            EditorApplication.playModeStateChanged -= OnPlayModeStateChange;
        }

        private void UpToolbar()
        {
            // 搜索框GUI
            m_TreeView.searchString = m_SearchField.OnGUI(new Rect(0, 0, position.width - 40f, 20f), m_TreeView.searchString);
        }

        private void TreeView()
        {
            m_TreeView.OnGUI(new Rect(0, 20f, position.width, position.height - 40f));
        }

        private void BottomToolBar()
        {
            GUILayout.BeginArea(new Rect(20f, position.height - 18f, position.width - 40f, 16f));

            using (new EditorGUILayout.HorizontalScope())
            {
                if (GUILayout.Button("展开所有"))
                {
                    m_TreeView.ExpandAll();
                }
                
                if (GUILayout.Button("收起所有"))
                {
                    m_TreeView.CollapseAll();
                }
            }
            
            GUILayout.EndArea();
        }
        
        private void OnPlayModeStateChange(PlayModeStateChange state)
        {
            switch (state)
            {
                case PlayModeStateChange.EnteredEditMode:
                    break;
                case PlayModeStateChange.ExitingEditMode:
                    break;
                case PlayModeStateChange.EnteredPlayMode:
                    break;
                case PlayModeStateChange.ExitingPlayMode:
                    m_window.Close();
                    break;
            }
        }
    }
}